import threading

def singleton(cls):  
    instances = {} 
    def get_instance(*args, **kwargs): 
        with threading.Lock(): 
            if cls not in instances:
                instances[cls] = cls(*args, **kwargs)  
        return instances[cls]  
    return get_instance  
  
@singleton  
class MyClass:  
    pass  
  
# 访问单例  
instance1 = MyClass()  
instance2 = MyClass()  
# assert instance1 is instance2  # 两者是同一个实例
print(id(instance1))
print(id(instance2))